/**
 * 
 */
package com.hanhai.zrb.mobile.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.hanhai.common.annotation.LoginNeedless;
import com.hanhai.common.web.controller.BaseController;
import com.hanhai.zrb.api.biz.service.UserDynamicService;
import com.hanhai.zrb.mobile.result.BaseApiResult;
import com.hanhai.zrb.mobile.util.FrontLoginUtil;
import com.hanhai.zrb.model.common.BaseUser;
import com.hanhai.zrb.model.common.PageList;
import com.hanhai.zrb.model.userDynamic.UserDynamic;
import com.hanhai.zrb.model.userDynamic.dto.UserDynamicDto;

/**
 * @author leiruiqi@hanhai.com
 *
 */
@RequestMapping("/dynamic")
@RestController
public class UserDynamicController extends BaseController{

	@Resource
	private UserDynamicService userDynamicService;
	
	/**
	 * 根据创建时间倒序分页查询所有平台的动态
	 * 
	 * @param createTime
	 * @param limit
	 * @return
	 */
	@LoginNeedless
	@RequestMapping("/queryPage")
	public BaseApiResult queryAllDynamic(Date createTime, Integer limit){
		BaseApiResult res = new BaseApiResult();
		UserDynamicDto dto = new UserDynamicDto();
		dto.setCreateTime(createTime);
		if(limit == null){
			limit = 10;
		}
		PageList<UserDynamic> pageList = userDynamicService.queryPageNoCountByDto(dto, 1, limit);
		List<UserDynamic> list = pageList.getList();
		userDynamicService.batchConverExtraDateForView(list);
		res.setData(list);
		return res;
	}
	
	/**
	 * 查询指定用户的所有动态
	 * 
	 * @param userId
	 * @return
	 */
	@RequestMapping("/queryAllByUid")
	public BaseApiResult queryAllDynamicByUserId(Long userId){
		BaseApiResult res = new BaseApiResult();
		
		List<UserDynamic> list = userDynamicService.queryAllByUserId(userId);
		removeExtra(list);
		res.setData(list);
		return res;
	}
	
	/**
	 * 查询我的所有动态
	 * 
	 * @return
	 */
	@RequestMapping("/queryMyDynamic")
	public BaseApiResult queryMyDynamic(){
		BaseApiResult res = new BaseApiResult();
		BaseUser user = FrontLoginUtil.getCurrentUser();
		List<UserDynamic> list = userDynamicService.queryAllByUserId(user.getUid());
		removeExtra(list);
		res.setData(list);
		return res;
	}
	
	private void removeExtra(List<UserDynamic> list){
		for(UserDynamic userDynamic:list){
			userDynamic.setExtra(null);
		}
	}
}
